<?php

class sortabletable {
	
	var $width;
	var $id;
	var $col_headers;
	var $col_sort_type;
	var $col_widths;
	var $zebra;
	var $rows;
	
	function sortabletable( $id = false, $arr_col_headers = false ) {
		$this->setWidth();
		$this->setId($id);
		$this->setColumnHeaders($arr_col_headers);
	}
	
	function setWidth($width = 600) {
		$this->width = $width;
	}
	
	function setId($id = false) {
		if (!$id) {
			$this->id = uniqid('table_');
		} else {
			$this->id = $id;
		}
	}
	
	function setColumnHeaders($arr_col_headers = false) {
		if (is_array($arr_col_headers)) {
			$this->col_headers = $arr_col_headers;
		}
	}
	
	function setColumnSortType($mix_col_sort_type = false) {
		if (is_array($mix_col_sort_type)) {
			$this->col_sort_type = $mix_col_sort_type;
		} else if (is_string($mix_col_sort_type)) {
			$this->col_sort_type = array_fill(0, count($this->col_headers), $mix_col_sort_type);
		}
	}
	
	function setColumnWidths($mix_col_widths = false) {
		if (is_array($mix_col_widths)) {
			$this->col_widths = $mix_col_widths;
		} else if (is_numeric($mix_col_widths)) {
			$this->col_widths = array_fill(0, count($this->col_headers), $mix_col_widths);
		}
	}
	
	function setZebra($bol_zebra) {
		$this->zebra = $bol_zebra;
	}
	
	function addRow ($arr_row) {
		$this->rows[] = $arr_row;
	}
	
	function render() {
		
		$columns = count($this->col_headers);
		
		// head
		$out  = "<table width=\"". $this->width ."\" cellpadding=\"0\" cellspacing=\"0\" id=\"". htmlspecialchars($this->id) ."\" class=\"sort-table\">";
		$out .= "<thead><tr>";
		
		for ($i = 0; $i < $columns; $i++) {
			$w = (is_numeric($this->col_widths[$i])) ? ' width="'.$this->col_widths[$i].'"' : '';
			$out .= "<td". $w .">". $this->col_headers[$i] ."</td>";
		}
		$out .= "</tr></thead>";
		// body
		$out .= "<tbody>";
		foreach ($this->rows as $row) {
			$out .= "<tr>";
			foreach ($row as $r) {
				$out .= "<td>". $r ."</td>";
			}
			$out .= "</tr>";
		}
		$out .= "</tbody>";
		$out .= "</table>\n";
		
		if ($this->zebra || count($this->col_sort_type) > 0) {
			$out .= "<script type=\"text/javascript\">\n";
			$out .= "<!--\n";
			if (DEBUG) $out .= "if (typeof(SortableTable) != 'function') alert('Warning!\\n\\nYou must include \"sortabletable.js\"\\nscript in order to use the table class.');\n";
			$out .= "var obj_". $this->id ." = new SortableTable(document.getElementById(\"". $this->id ."\"), ";
			
			if (is_array($this->col_sort_type)) {
				foreach ($this->col_sort_type as $cst) {
					$o[] .= "\"". $cst ."\"";
				}
			} else {
				for ($i = 0; $i < $columns; $i++) {
					$o[] = "\"None\"";
				}
			}
			
			$out .= "[". (implode(", ", $o)) ."]";
			$out .= ");\n";
			
			if ($this->zebra) {
				$out .= "zebrate(obj_". $this->id .");\n";
				$out .= "obj_". $this->id .".onsort = function() { zebrate(obj_". $this->id ."); }\n";
			}
			
			$out .= "//-->\n";
			$out .= "</script>\n";
		}
		return $out;
	}
	
}

?>